home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / print.idb / usr / sbin / mknetpr.z / mknetpr
Text File  |  1998-05-04  |  16KB  |  597 lines

  1. #!/bin/sh
  2. #Tag 0x00000700
  3. #**************************************************************************
  4. #*
  5. #*           Copyright (c) 1993 Silicon Graphics, Inc.
  6. #*            All Rights Reserved
  7. #*
  8. #*       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  9. #*
  10. #* The copyright notice above does not evidence any actual of intended
  11. #* publication of such source code, and is an unpublished work by Silicon
  12. #* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  13. #* the property of Silicon Graphics, Inc. Any use, duplication or
  14. #* disclosure not specifically authorized by Silicon Graphics is strictly
  15. #* prohibited.
  16. #*
  17. #* RESTRICTED RIGHTS LEGEND:
  18. #*
  19. #* Use, duplication or disclosure by the Government is subject to
  20. #* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  21. #* Technical Data and Computer Software clause at DFARS 52.227-7013,
  22. #* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  23. #* Supplement. Unpublished - rights reserved under the Copyright Laws of
  24. #* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  25. #* Shoreline Blvd., Mountain View, CA 94039-7311
  26. #**************************************************************************
  27. #*
  28. #* File: mknetpr
  29. #*
  30. #* $Revision: 1.26 $
  31. #*
  32. #* Description: Shell script for adding a remote printer to the local
  33. #*    System V spooling system.
  34. #*
  35. #*    Usage: mknetpr [<local printer name>] [<remote host>]
  36. #*               [<remote printer name>]
  37. #*
  38. #*    If the local printer name, remote printer name and remote
  39. #*    printer host are all specified on the command line, mknetpr does
  40. #*    not prompt for input and terminates upon encountering errors. If the
  41. #*    command line information is not complete, mknetpr runs interactively
  42. #*    prompting for the missing information and allowing recovery from
  43. #*    certain installation error conditions.
  44. #*
  45. #**************************************************************************
  46.  
  47.  
  48. # Well known directories and programs
  49.  
  50. LPUTIL_DIR=/usr/lib
  51. LPUTIL=$LPUTIL_DIR/lputil
  52. OUT_FILT="/usr/bin/pg -s -p more... -e"
  53. SPOOL_DIR=/var/spool/lp
  54. LPSTAT=/usr/bin/lpstat
  55. MKJTPR=/usr/sbin/mkjtpr
  56. MKBSDPR=/usr/sbin/mkbsdnetpr
  57. AWK_DIR=/usr/bin
  58. AWK=$AWK_DIR/nawk
  59.  
  60. # Global variables
  61.  
  62. LOCAL_PNAME=""
  63. REMOTE_PNAME=""
  64. REMOTE_HOST=""
  65. INTERACT=0
  66. REMOTE_ID="lp"
  67.  
  68.  
  69. #
  70. # Prints the program usage string
  71. #
  72. PrintUsage()
  73. {
  74.     echo "Usage: mknetpr [<local printer name>] [<remote host>] \c"
  75.     echo "[<remote printer name>]"
  76. }
  77.  
  78.  
  79. #
  80. # Checks that we are the super-user and verifies
  81. # that the System V spooling system is present
  82. #
  83. CheckPermSpooler()
  84. {
  85.     idstr=`id`
  86.     cid=`expr "$idstr" : '^[     ]*uid=.*(\(.*\))[     ]*gid='`
  87.     if [ "$cid" != "root" ]; then
  88.         echo "You must be logged in as root to use this program."
  89.         exit 1;
  90.     fi
  91.     if [ ! -r $SPOOL_DIR ]; then
  92.         echo "The System V spooling system is not installed on this system."
  93.         exit 1
  94.     fi
  95. }
  96.  
  97. #
  98. # Check for a duplicate printer name. Returns 0 if dup,
  99. # 1 if name is unique
  100. #
  101. CheckDupName()
  102. {
  103.     /bin/ls $SPOOL_DIR/member/$1 2> /dev/null 1> /dev/null
  104.     return $?
  105. }
  106.  
  107.  
  108. #
  109. # Query user for a name for the local printer
  110. #
  111. QueryLocalName()
  112. {
  113.     while [ "$LOCAL_PNAME" = "" ]; do
  114.         echo "Enter new printer name (14 chars max.): \c"
  115.         read LOCAL_PNAME
  116.         VerifyPname $LOCAL_PNAME
  117.         if [ $? -eq 1 ]; then
  118.         echo "Invalid response: Name must be 14 chars (A-Za-z0-9_ only)"
  119.         LOCAL_PNAME=""
  120.         fi
  121.         if [ "$LOCAL_PNAME" = "" ]; then
  122.         continue
  123.         fi
  124.     CheckDupName $LOCAL_PNAME
  125.         if [ $? -eq 0 ]; then
  126.         echo " "
  127.             echo "WARNING: Printer $LOCAL_PNAME already exists."
  128.             echo '\nChoose a new name (y/n)[y]? \c'
  129.             read yn
  130.             if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
  131.             LOCAL_PNAME=""
  132.             fi
  133.         fi
  134.     done
  135. }
  136.  
  137.  
  138. #
  139. # Test for pure decimal positive integer input
  140. #
  141. # Expects $1 to be the value to check. Returns 0 if
  142. # OK, 1 if invalid positive integer or if more than one
  143. # argument specified
  144. #
  145. VerifyInt()
  146. {
  147.     if [ $# -eq 0 ]; then
  148.     return 0
  149.     fi
  150.     if [ $# -gt 1 ]; then
  151.     return 1
  152.     fi
  153.     echo $1 | egrep -s '^[0-9]*$'
  154.     return $?
  155. }
  156.  
  157.  
  158. #
  159. # Test for a valid printer name (i.e. 14 chars max, A-Za-z0-9_
  160. # only and no spaces)
  161. #
  162. VerifyPname()
  163. {
  164.     if [ $# -eq 0 ]; then
  165.     return 0
  166.     fi
  167.     if [ $# -gt 1 ]; then
  168.     return 1
  169.     fi
  170.     echo $1 | egrep -s '^[A-Za-z0-9_]*$'
  171.     if [ $? -eq 1 ]; then
  172.     return 1
  173.     fi
  174.     if [ `expr $1 : '.*'`  -gt 14 ]; then
  175.     return 1
  176.     fi
  177.     return 0
  178. }
  179.  
  180.  
  181. #
  182. # Test for a properly constructed remote host name
  183. #
  184. VerifyHostname()
  185. {
  186.     if [ $# -gt 1 ]; then
  187.     return 1
  188.     fi
  189.     return 0
  190. }
  191.  
  192.  
  193. #########################################################################
  194. #
  195. # Main program
  196. #
  197.  
  198. #
  199. # Ensure that we are root and that the main System V
  200. # spooling system directory is present
  201. #
  202. CheckPermSpooler
  203.  
  204. #
  205. # Get command line arguments, if any, and determine if we
  206. # should run in interactive mode
  207. #
  208. case $# in
  209.     0) INTERACT=1
  210.        ;;
  211.     1) INTERACT=1
  212.        LOCAL_PNAME=$1
  213.        ;;
  214.     2) INTERACT=1
  215.        LOCAL_PNAME=$1
  216.        REMOTE_HOST=$2
  217.        ;;
  218.     3) INTERACT=0
  219.        LOCAL_PNAME=$1
  220.        REMOTE_HOST=$2
  221.        REMOTE_PNAME=$3
  222.        ;;
  223.     *) PrintUsage
  224.        exit 1
  225.        ;;
  226. esac
  227.  
  228. #
  229. # Display an intro message
  230. #
  231. echo "Remote Printer Installation Tool\n"
  232.  
  233. # Ask user if this printer is directly on the network.  We handle that
  234. # with mkjtpr.  ONLY ask if INTERACTive is false because this script
  235. # can be called from the printer manager with all required arguments 
  236. # and in that case it should not query a user for info.  Test for mkjtpr
  237. # because it is part of Impressario and might not be installed (skip
  238. # this if not installed).
  239.  
  240. if [ "$INTERACT" != "0" ]; then
  241.      while [ "$NETPRINTER" = "" ]; do
  242.         echo "Spool files:"
  243.         echo ""
  244.         echo "1. To another SGI workstation?"
  245.         echo " "
  246.         echo "2. A printer connected to a non-SGI workstation that supports the "
  247.         echo "   BSD printing protocols (files will be converted to PostScript"
  248.         echo "   before being sent to a BSD print spooler)?"
  249.         echo " "
  250.         if [ -x $MKJTPR ]; then
  251.            echo "3. Directly to a network printer.  A network printer is a printer"        
  252.            echo "   connected to the network with a network card or network adaptor"
  253.            echo "   (i.e. an HP JetDirect card)?"
  254.            echo " "
  255.         fi
  256.  
  257.         echo "Enter selection:\c"
  258.         read NETPRINTER
  259.         VerifyInt $NETPRINTER
  260.         if [ $? -eq 1 ]; then
  261.              echo "Invalid response."
  262.              NETPRINTER=""
  263.         fi
  264.         if [ "$NETPRINTER" = "" ]; then
  265.              continue
  266.         fi
  267.  
  268.         if [ \( "$NETPRINTER" != "1" \) -a \( "$NETPRINTER"  != "2" \)  -a \( "$NETPRINTER"  != "3" \) ]; then
  269.              echo "Invalid response."
  270.              NETPRINTER=""
  271.         fi
  272.  
  273.         if [ "$NETPRINTER" = "3" ]; then
  274.            if [ -x $MKJTPR ]; then
  275.               $MKJTPR  "$LOCAL_PNAME" "$REMOTE_HOST"
  276.               exit $?
  277.            else
  278.               echo "Invalid response."
  279.               NETPRINTER=""
  280.               continue
  281.            fi   
  282.         fi
  283.  
  284.         if [ "$NETPRINTER" = "2" ]; then
  285.            $MKBSDPR  "$LOCAL_PNAME" "$REMOTE_HOST"
  286.            exit $?
  287.         fi
  288.  
  289.      done
  290. fi
  291.  
  292. #
  293. # Make sure we have a remote printer host
  294. #
  295. if [ "$REMOTE_HOST" = "" ]; then
  296.     while [ "$REMOTE_HOST" = "" ]; do
  297.         echo "Enter remote host machine name: \c"
  298.         read REMOTE_HOST
  299.         VerifyHostname $REMOTE_HOST
  300.         if [ $? -eq 1 ]; then
  301.         echo "Invalid response: Improperly constructed hostname"
  302.         REMOTE_HOST=""
  303.         fi
  304.     done
  305.     echo " "
  306. fi
  307.  
  308. #
  309. # First test that we can talk to the remote host
  310. #
  311. echo "Testing connection to remote host $REMOTE_HOST...\n"
  312.  
  313. # LPUTIL chkremote runs /usr/lib/print/listprinters 
  314.  
  315. TMP_NAMES=`$LPUTIL chkremote "$REMOTE_HOST" "$REMOTE_ID"`
  316. if [ $? -ne 0 ]; then
  317.     echo "Failed to connect to $REMOTE_HOST"
  318.     echo "Check that remote machine is up and allows login as $REMOTE_ID"
  319.     echo "You may need to run \"addclient(1M)\" on $REMOTE_HOST\n"
  320.     exit 1
  321. fi
  322.  
  323. # Depending on the version of the lp system on the remote SGI.
  324. # At this point, TMP_NAMES may be:
  325. #
  326. # 5N\n
  327. # 5M\n
  328. # dumb\n
  329. # plotter\n
  330. #
  331. # Or it may be:
  332. #
  333. # twister MonoPostScript remote thurgood twister sgi
  334. # tornado Raster remote thurgood tornado sgi
  335. # 5N Raster local
  336. # 5M MonoPostScript local
  337. # testbsd PostScript remote imprtest2 raw bsd
  338. # dumb PostScript local
  339. # plotter ColorRaster local
  340. # torn Raster remote thurgood tornado sgi
  341. #
  342. # Get just the first field from each line (which is the printer name)
  343. REMOTE_NAMES=`echo "$TMP_NAMES" | $AWK '{print $1}'`
  344.  
  345. #
  346. # Parse out the remaining data (if it is there).  Missing fields become "<unknown>".
  347. # After this you would have (note how the "<unknowns> keep evrything allinged
  348. # so element 5 in the remote names list is correct for element 5 in all lists):
  349. #
  350. # twister tornado 5N 5M testbsd dumb plotter torn
  351. # MonoPostScript Raster Raster MonoPostScript PostScript PostScript ColorRaster  Raster
  352. # remote remote local local remote local local remote
  353. # thurgood tornado <unknown> <unknown> imprtest2 <unknown> <unknown>  thurgood
  354. # ...
  355. #
  356. REMOTE_TYPES=`echo "$TMP_NAMES"       | $AWK '{if ($2!="") print $2; else print "<unknown>";}'`
  357. REMOTE_CONNECTIONS=`echo "$TMP_NAMES" | $AWK '{if ($3!="") print $3; else print "<unknown>";}'`
  358. REMOTE_TRUE_HOSTS=`echo "$TMP_NAMES"  | $AWK '{if ($4!="") print $4; else print "<unknown>";}'`
  359. REMOTE_TRUE_NAMES=`echo "$TMP_NAMES"  | $AWK '{if ($5!="") print $5; else print "<unknown>";}'`
  360. REMOTE_CONNTYPES=`echo "$TMP_NAMES"   | $AWK '{if ($6!="") print $6; else print "<unknown>";}'`
  361.  
  362. #
  363. # Now make sure there are printers on the remote host
  364. #
  365. if [ "$REMOTE_NAMES" = "" ]; then
  366.     echo "There are either no printers connected to $REMOTE_HOST"
  367.     echo "or the network connection to $REMOTE_HOST has failed. Check"
  368.     echo "that the remote machine is up and has at least one printer"
  369.     echo "connected."
  370.     exit 1
  371. fi
  372.  
  373. echo "Connection to $REMOTE_HOST verified\n"
  374.  
  375. #
  376. # Query user for a name for this printer
  377. #
  378. if [ "$LOCAL_PNAME" = "" ]; then
  379.     QueryLocalName
  380.     echo " "
  381. else
  382.     echo "Requested local printer name: $LOCAL_PNAME\n"
  383.     VerifyPname $LOCAL_PNAME
  384.     if [ $? -eq 1 ]; then
  385.     echo "Error: Local printer name must be 14 chars (A-Za-z0-9_ only)"
  386.     if [ $INTERACT -eq 1 ]; then
  387.         LOCAL_PNAME=""
  388.         QueryLocalName
  389.         echo " "
  390.     else
  391.         exit 1
  392.     fi
  393.     fi
  394.     CheckDupName $LOCAL_PNAME
  395.     if [ \( $? -eq 0 \) -a \( $INTERACT -eq 1 \) ]; then
  396.     echo " "
  397.         echo "WARNING: Printer $LOCAL_PNAME already exists."
  398.         echo '\nChoose a new name (y/n)[y]? \c'
  399.         read yn
  400.         if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
  401.         LOCAL_PNAME=""
  402.         QueryLocalName
  403.         echo " "
  404.         fi
  405.     fi
  406. fi
  407.  
  408. #
  409. # Now select the remote printer
  410. #
  411. if [ "$REMOTE_PNAME" = "" ]; then
  412.     echo "Printers on $REMOTE_HOST are:"
  413.     nump=0
  414.  
  415.     for pname in $REMOTE_NAMES
  416.     do
  417.     nump=`expr $nump + 1`
  418.     echo "\t$nump. $pname"
  419.     done
  420.     rnum=""
  421.     while [ "$rnum" = "" ]; do
  422.         echo "Enter number of printer to install: \c"
  423.         read rnum
  424.         VerifyInt $rnum
  425.         if [ $? -eq 1 ]; then
  426.             echo "Invalid response: Printer number must be between 1 and $nump"
  427.             rnum=""
  428.         fi
  429.         if [ "$rnum" = "" ]; then
  430.             continue
  431.         fi
  432.         if [ \( $rnum -eq 0 \) -o \( $rnum -gt $nump \) ]; then
  433.             echo "Invalid response: Number must be between 1 and $nump"
  434.             rnum=""
  435.         fi
  436.     done
  437.     cnt=0
  438.     for pname in $REMOTE_NAMES
  439.     do
  440.     cnt=`expr $cnt + 1`
  441.     if [ $cnt -eq $rnum ]; then
  442.         REMOTE_PNAME=$pname
  443.         break
  444.     fi
  445.     done
  446.  
  447.  
  448. #   Check to see if the remote printer is itself a network printer. If so,
  449. #   determine who the true server is and save it.
  450.  
  451.     cnt=0
  452.     TRUE_REMOTE_HOST=""
  453.     for pname in $REMOTE_TRUE_HOSTS
  454.     do
  455.         cnt=`expr $cnt + 1`
  456.         if [ $cnt -eq $rnum ]; then
  457.             if [ $pname != "<unknown>" ]; then
  458.                  TRUE_REMOTE_HOST=$pname
  459.             fi
  460.             break
  461.         fi
  462.     done
  463.  
  464. #   Get the real remote printer name if required.  Example:
  465. #   host: thurgood  printer: test1 but test1 is a network printer to
  466. #   the host lodi printer 5MP.  In above block we re-mapped thurgood
  467. #   to lodi and now we remap test1 to 5MP.
  468.  
  469.     cnt=0
  470.     TRUE_REMOTE_PNAME=""
  471.     for pname in $REMOTE_TRUE_NAMES
  472.     do
  473.         cnt=`expr $cnt + 1`
  474.         if [ $cnt -eq $rnum ]; then
  475.             if [ $pname != "<unknown>" ]; then
  476.                TRUE_REMOTE_PNAME=$pname
  477.             fi
  478.             break
  479.         fi
  480.     done
  481.  
  482.     cnt=0
  483.     CONNTYPE=""
  484.     for pname in $REMOTE_CONNTYPES
  485.     do
  486.         cnt=`expr $cnt + 1`
  487.         if [ $cnt -eq $rnum ]; then
  488.             if [ $pname != "<unknown>" ]; then
  489.                TRUE_CONNTYPE=$pname
  490.             fi
  491.             break
  492.         fi
  493.     done
  494.  
  495.     if [ $TRUE_REMOTE_HOST ]; then
  496.         if [ $TRUE_REMOTE_PNAME ]; then
  497.             echo ""
  498.             echo "Warning: You are configuring a multi-hop printer."
  499.             echo "The printer $REMOTE_PNAME on host $REMOTE_HOST actually sends print jobs"
  500.             echo "to the printer $TRUE_REMOTE_PNAME on host $TRUE_REMOTE_HOST (resulting in"
  501.             echo "multiple hops to get to the actual print server). It would probably be"
  502.             echo "best if the printer you are configuring ($LOCAL_PNAME) also sent print"
  503.             echo "jobs directly to the printer $TRUE_REMOTE_PNAME on host $TRUE_REMOTE_HOST."
  504.             echo ""
  505.             echo "Do you want the print jobs for the new printer $LOCAL_PNAME sent to the"
  506.             echo "printer $TRUE_REMOTE_PNAME on host $TRUE_REMOTE_HOST (y/n)[y]? \c'"
  507.             read yn
  508.             if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
  509.                 REMOTE_PNAME="$TRUE_REMOTE_PNAME"
  510.                 REMOTE_HOST="$TRUE_REMOTE_HOST"
  511.                 CONNTYPE="$TRUE_CONNTYPE"
  512.             fi
  513.         fi
  514.     fi
  515.  
  516. else
  517.     foundit=0
  518.     for pname in $REMOTE_NAMES
  519.     do
  520.     if [ "$pname" = "$REMOTE_PNAME" ]; then
  521.         foundit=1
  522.         break
  523.     fi
  524.     done
  525.     if [ $foundit -eq 0 ]; then
  526.     echo "Remote printer $REMOTE_PNAME not found on $REMOTE_HOST"
  527.     echo "Known printers on $REMOTE_HOST are:\n"
  528.     echo "$REMOTE_NAMES\n"
  529.     exit 1
  530.     fi
  531. fi
  532.  
  533. #
  534. # Inform the user of what comes next
  535. #
  536. echo " "
  537. echo "Installing remote printer: $LOCAL_PNAME"
  538. echo "Remote print server used: $REMOTE_HOST"
  539. echo "Remote spooler name used: $REMOTE_PNAME"
  540. echo " "
  541.  
  542. #
  543. # Get the type and model name of the remote printer
  544. #
  545. ##rinfo=`$LPUTIL remoteinfo $REMOTE_HOST $REMOTE_PNAME $REMOTE_ID`
  546. ##
  547. ##if [ \( $? -ne 0 \) -o \( "$rinfo" = "" \) ]; then
  548. ##    echo "Could not get remote printer information"
  549. ##    echo "Check network connection and printer installation on remote host"
  550. ##    exit 1
  551. ##fi
  552. ### Change IFS to parse newlines as separators.
  553. ##oldIFS=$IFS
  554. ##IFS='
  555. ##'
  556. ##for item in $rinfo
  557. ##do
  558. ##    val=`echo $item | egrep '^NAME='`
  559. ##    if [ "$val" != "" ]; then
  560. ##    NFLAG=$val
  561. ##    else
  562. ##       val=`echo $item | egrep '^TYPE='`
  563. ##        if [ "$val" != "" ]; then
  564. ##        TFLAG=$val
  565. ##    fi
  566. ##    fi
  567. ##done
  568. ##IFS=$oldIFS
  569.  
  570. #
  571. # Install the printer.
  572. #
  573. if [ CONNTYPE ]; then
  574.     $LPUTIL addnet $REMOTE_HOST $REMOTE_PNAME $LOCAL_PNAME $CONNTYPE
  575. else
  576.     $LPUTIL addnet $REMOTE_HOST $REMOTE_PNAME $LOCAL_PNAME 
  577. fi
  578. if [ $? -ne 0 ]; then
  579.     echo "ERROR: Installation of new printer failed."
  580.     echo "       $LPUTIL command failed."
  581.     exit 1
  582. fi
  583.  
  584. #
  585. # Inform the user of what has happened
  586. #
  587. echo "Remote printer $LOCAL_PNAME has been installed"
  588. echo " "
  589. echo "Here is your printing environment:"
  590. echo " "
  591. $LPSTAT -t | $OUT_FILT
  592.  
  593. #
  594. # Return a successful exit code
  595. #
  596. exit 0
  597.